//
//  ListOfMegafaunaOfWeekTableViewCell.m
//  Cachalot
//
//  Created by Adam Cue on 6/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListOfMegafaunaOfWeekTableViewCell.h"
#import "MFOWMedia.h"

#define NAME_LABEL_FRAME         CGRectMake(70, 3, self.contentView.frame.size.width - 70, 20)
#define DATE_LABEL_FRAME         CGRectMake(70, 23, self.contentView.frame.size.width - 70, 19)

@interface ListOfMegafaunaOfWeekTableViewCell (Private)
#pragma mark SetupMethods
- (void) setupTextLabels;
- (void) setupImageView;
@end

@implementation ListOfMegafaunaOfWeekTableViewCell

@synthesize nameLabel = _nameLabel;
@synthesize dateLabel = _dateLabel;
@synthesize asyncImageView = _asyncImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
      [self setupTextLabels];
      [self setupImageView];
   }
   return self;
}

- (void)prepareForReuse {
   [super prepareForReuse];
   
   //[self.asyncImageView setDisplayImage:nil];
}

- (void)dealloc {
   self.nameLabel = nil;
   self.dateLabel = nil;
   self.asyncImageView = nil;
   
   [super dealloc];
}

#pragma mark SetupMethods
- (void)setupTextLabels {
   _nameLabel = [[UILabel alloc] initWithFrame:NAME_LABEL_FRAME];
   self.nameLabel.textColor = [UIColor whiteColor];
   self.nameLabel.font = [UIFont boldSystemFontOfSize:18];
   self.nameLabel.backgroundColor = [UIColor clearColor];
   [self.contentView addSubview:self.nameLabel];
   
   _dateLabel = [[UILabel alloc] initWithFrame:DATE_LABEL_FRAME];
   self.dateLabel.textColor = [UIColor grayColor];
   self.dateLabel.font = [UIFont systemFontOfSize:14];
   self.dateLabel.backgroundColor = [UIColor clearColor];
   [self.contentView addSubview:self.dateLabel];
}

- (void)setupImageView {
   _asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 60, self.contentView.frame.size.height)];
   [self.contentView addSubview:self.asyncImageView];
}

#pragma mark ExternalMethods
+ (void)fillCell:(ListOfMegafaunaOfWeekTableViewCell *)cell withAllMegafauna:(NSDictionary *)info{
   cell.nameLabel.text = [info objectForKey:@"name"];
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd"];
   NSDate *date = [dateFormatter dateFromString:[info objectForKey:@"date"]];
   [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
   cell.dateLabel.text = [dateFormatter stringFromDate:date];
   [dateFormatter release];
   [cell.asyncImageView loadImageFromURL:[NSURL URLWithString:[info objectForKey:@"preview"]] withDefaultImage:nil];
}

+ (void)fillCell:(ListOfMegafaunaOfWeekTableViewCell *)cell withSavedMegafauna:(MFOW *)mfow{
   cell.nameLabel.text = mfow.name;
   cell.dateLabel.text = @"";
   MFOWMedia * med = [[mfow.media filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"isMain == %@", [NSNumber numberWithBool:YES]]] anyObject];
   if(med.fullImage != nil) {
      [cell.asyncImageView setDisplayImage:[UIImage imageWithData:med.fullImage]];
   }
   else {
      [cell.asyncImageView setDisplayImage:[UIImage imageWithData:med.thumbnailImage]];
   }
}

@end
